package job.work.domain.system.entity.pojo.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.hibernate.annotations.Proxy;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Set;

@Data
@Entity //代表当前是实体类
@Table(name="dec_system")//对应mysql 表名
@ApiModel("用户登录表")
@Proxy(lazy = false)//禁止懒加载
@ToString
public class  SystemUser implements Serializable, UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    /**
     * 用户名
     */
    @Column(name = "username")
    @ApiModelProperty(value = "用户名",required = true)
    @NotBlank(message = "用户名必填")
    private String username;
    /**
     * 密码
     */
    @Column(name = "password")
    @ApiModelProperty(value = "密码")
    private String password;
    /**
     * 角色ID
     */
    @Column(name = "role_id")
    @ApiModelProperty(value = "角色ID 0代表普通用户",required = true)
    private Long roleId;

    /**
     * 若type 为1 时 则需要修改密码
     */
    @Column(name = "type")
    private Integer type;

    /**
     * 手机号
     */
    @Column(name = "phone")
    @NotBlank(message = "手机号必填")
    @ApiModelProperty(value = "手机号",required = true)
    private String phone;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /**
     * 最后一次登陆时间
     */
    @Column(name = "last_login_time")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lastLoginTime;

    /**
     *  0启用 1 禁用
     */
    @ApiModelProperty(value = "0启用 1 禁用")
    @Column(name = "status")
    private Integer status;

    /**
     * 性别 1男 2 女 0 其他
     */
    @ApiModelProperty(value = "性别 1男 2 女 0 其他")
    @Column(name = "gender")
    private Integer gender;

     /**
     * 用户支付密码
     */
    @Column(name = "pay_password")
    @ApiModelProperty(value = "用户支付密码")
    private String payPassword;

    /**
     * 微信昵称
     */
    @Column(name = "wx_nick_name")
    @ApiModelProperty(value = "微信昵称")
    private String wxNickName;
   /**
     * QQ昵称
     */
   @Column(name = "qq_nick_name")
    @ApiModelProperty(value = "QQ昵称")
    private String qqNickName;

    /**
     * 真实姓名
     */
    @Column(name = "real_name")
    private String realName;
    /**
     *用户头像
     */
    @Column(name = "image")
    private String image;

    @Column(name = "point")
    private Integer point;

    /**
     *用户钱包
     */
    @Column(name = "wallet")
    private BigDecimal wallet;
    @Transient//忽略实体类
    private Collection<? extends GrantedAuthority> authorities;

    @Transient//忽略实体类
    private Set<String> userPermission;

    @Transient//忽略实体类
    private int rememberMe;


    // 写一个能直接使用user创建jwtUser的构造器
    public SystemUser(SystemUser user) {
        id = user.getId();
        username = user.getUsername();
        password = user.getPassword();
        roleId = user.getRoleId();
        authorities = Collections.singleton(new SimpleGrantedAuthority(String.valueOf(user.getRoleId())));
    }

    public SystemUser() {
    }

    public SystemUser(String username, String password, Long roleId, Integer type, String phone, Date createTime, Date lastLoginTime, Integer status, Integer gender, String payPassword, String wxNickName, String qqNickName, String realName, String image, BigDecimal wallet, Collection<? extends GrantedAuthority> authorities, int rememberMe) {
        this.username = username;
        this.password = password;
        this.roleId = roleId;
        this.type = type;
        this.phone = phone;
        this.createTime = createTime;
        this.lastLoginTime = lastLoginTime;
        this.status = status;
        this.gender = gender;
        this.payPassword = payPassword;
        this.wxNickName = wxNickName;
        this.qqNickName = qqNickName;
        this.realName = realName;
        this.image = image;
        this.wallet = wallet;
        this.authorities = authorities;
        this.rememberMe = rememberMe;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
